package cn.iocoder.yudao.module.blog.controller.admin.improvechapter;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.improvechapter.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvechapter.BlogImproveChapterDO;
import cn.iocoder.yudao.module.blog.convert.improvechapter.BlogImproveChapterConvert;
import cn.iocoder.yudao.module.blog.service.improvechapter.BlogImproveChapterService;

@Tag(name = "管理后台 - 课程章节")
@RestController
@RequestMapping("/blog/improve-chapter")
@Validated
public class BlogImproveChapterController {

    @Resource
    private BlogImproveChapterService improveChapterService;

    @PostMapping("/create")
    @Operation(summary = "创建课程章节")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:create')")
    public CommonResult<Long> createImproveChapter(@Valid @RequestBody BlogImproveChapterCreateReqVO createReqVO) {
        return success(improveChapterService.createImproveChapter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程章节")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:update')")
    public CommonResult<Boolean> updateImproveChapter(@Valid @RequestBody BlogImproveChapterUpdateReqVO updateReqVO) {
        improveChapterService.updateImproveChapter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程章节")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:delete')")
    public CommonResult<Boolean> deleteImproveChapter(@RequestParam("id") Long id) {
        improveChapterService.deleteImproveChapter(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程章节")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:query')")
    public CommonResult<BlogImproveChapterRespVO> getImproveChapter(@RequestParam("id") Long id) {
        BlogImproveChapterDO improveChapter = improveChapterService.getImproveChapter(id);
        return success(BlogImproveChapterConvert.INSTANCE.convert(improveChapter));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程章节列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:query')")
    public CommonResult<List<BlogImproveChapterRespVO>> getImproveChapterList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogImproveChapterDO> list = improveChapterService.getImproveChapterList(ids);
        return success(BlogImproveChapterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程章节分页")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:query')")
    public CommonResult<PageResult<BlogImproveChapterRespVO>> getImproveChapterPage(@Valid BlogImproveChapterPageReqVO pageVO) {
        PageResult<BlogImproveChapterDO> pageResult = improveChapterService.getImproveChapterPage(pageVO);
        return success(BlogImproveChapterConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程章节 Excel")
    @PreAuthorize("@ss.hasPermission('blog:improve-chapter:export')")
    @OperateLog(type = EXPORT)
    public void exportImproveChapterExcel(@Valid BlogImproveChapterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogImproveChapterDO> list = improveChapterService.getImproveChapterList(exportReqVO);
        // 导出 Excel
        List<BlogImproveChapterExcelVO> datas = BlogImproveChapterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程章节.xls", "数据", BlogImproveChapterExcelVO.class, datas);
    }

}
